<?php

namespace App\Http\Controllers\Admin\SharePrize;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class PrizeController extends BaseController
{

    /**
     * Note: 中奖记录
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {get} /shareprize/prize/prizerecord
     */
    public function prizeRecord(Request $request)
    {
        $param = $request->all();

        $v = app('validator')->make($param, [
            'page' => 'required|integer|min:1',
            'page_size' => 'required|integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $todayStartTime = date("Y-m-d 00:00:00");
        $todayEndTime = date("Y-m-d 23:59:59");
        $todayWhere['is_finished'] = 1;
        $todayWhere[] = ['prize_time', ">=", $todayStartTime];
        $todayWhere[] = ['prize_time', "<=", $todayEndTime];
        //今日抽奖次数
        $res['today_number'] = app('model_shareprize_get_record')->where($todayWhere)->count();
        //今日抽奖总价值
        $res['today_cost'] = app('model_shareprize_get_record')->where($todayWhere)->sum('cost');

        //中奖记录
        $where['a.is_finished'] = 1;

        //筛选
        if (!empty($param['user_code'])) {
            $where['b.user_code'] = $param['user_code'];
        }
        if (!empty($param['nickname'])) {
            $where[] = ['b.nickname', 'like', '%' . $param['nickname'] . '%'];
        }
        if (!empty($param['start_time'])) {
            $where[] = ['a.prize_time', ">=", $param['start_time']];
        }
        if (!empty($param['end_time'])) {
            $where[] = ['a.prize_time', "<=", $param['end_time']];
        }

        $res['list'] = app('model_shareprize_get_record')
            ->from('ln_shareprize_get_record as a')
            ->join('ln_user_data as b', 'a.user_id', '=', 'b.user_id')
            ->select([
                'a.id', 'a.user_id',  'b.user_code', 'b.nickname', 'a.product_name', 'a.prize_choose_type', 'a.number',  'a.cost', 'a.prize_time'
            ])
            ->where($where)
            ->orderBy('a.id', 'desc')
            ->paginate($param['page_size'], ['*'], '', $param['page']);

        foreach ($res['list'] as &$value) {
            switch ($value->prize_choose_type) {
                case '1':
                    $value->level = '普通';
                    break;
                case '2':
                    $value->level = '稀有';
                    break;
                case '3':
                    $value->level = '史诗';
                    break;
                case '4':
                    $value->level = '传说';
                    break;
            }
        }


        return Response::sendData($res);
    }

    /**
     * Note: 邀请人列表
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {get} /shareprize/prize/friendvaluerecord
     */
    public function friendvalueRecord(Request $request)
    {
        $param = $request->all();

        $v = app('validator')->make($param, [
            'page' => 'required|integer|min:1',
            'page_size' => 'required|integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $where = [];
        //筛选
        if (!empty($param['user_code'])) {
            $where['b.user_code'] = $param['user_code'];
        }
        if (!empty($param['nickname'])) {
            $where[] = ['b.nickname', 'like', '%' . $param['nickname'] . '%'];
        }


        $list = app('model_shareprize_user_record')
            ->from('ln_shareprize_user_record as a')
            ->join('ln_user_data as b', 'a.share_user_id', '=', 'b.user_id')
            ->select([
                'a.id', 'a.share_user_id', 'b.user_code', 'b.nickname',  'a.created_at'

            ])
            ->where($where)
            ->groupBy('a.share_user_id')
            ->orderBy('a.id', 'desc')
            ->paginate($param['page_size'], ['*'], '', $param['page']);

        foreach ($list as &$value) {
            $value['number'] =  app('model_shareprize_user_record')->where(['share_user_id' => $value->share_user_id])->count();
            $value['friend_value'] = $value['number'] * 100;
            $value['created_at'] = app('model_shareprize_user_record')->where(['share_user_id' => $value->share_user_id])->orderBy('created_at', 'desc')->value('created_at');
        }

        return Response::sendData($list);
    }
    /**
     * Note: 被邀请人列表
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {get} /shareprize/prize/sharerecord
     */
    public function shareRecord(Request $request)
    {

        $param = $request->all();

        $v = app('validator')->make($param, [
            'share_user_id' => 'required',
            'page' => 'required|integer|min:1',
            'page_size' => 'required|integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $todayStartTime = date("Y-m-d 00:00:00");
        $todayWhere['share_user_id'] = $param['share_user_id'];
        $todayWhere[] = ['created_at', ">=", $todayStartTime];

        //今日邀请人数
        $res['today_number'] = app('model_shareprize_user_record')->where($todayWhere)->count();
        $sevendayStartTime = date("Y-m-d 00:00:00", strtotime("-7 day"));
        $sevendayWhere['share_user_id'] = $param['share_user_id'];
        $sevendayWhere[] = ['created_at', ">=", $sevendayStartTime];
        //7日邀请人数
        $res['sevenday_number'] = app('model_shareprize_user_record')->where($sevendayWhere)->count();

        $thirtydayStartTime = date("Y-m-d 00:00:00", strtotime("-30 day"));
        $thirtydayWhere['share_user_id'] = $param['share_user_id'];
        $thirtydayWhere[] = ['created_at', ">=", $thirtydayStartTime];
        //30日邀请人数
        $res['thirtyday_number'] = app('model_shareprize_user_record')->where($thirtydayWhere)->count();


        $where['a.share_user_id'] = $param['share_user_id'];
        //筛选
        if (!empty($param['user_code'])) {
            $where['b.user_code'] = $param['user_code'];
        }
        if (!empty($param['nickname'])) {
            $where[] = ['b.nickname', 'like', '%' . $param['nickname'] . '%'];
        }
        if (!empty($param['start_time'])) {
            $where[] = ['a.created_at', ">=", $param['start_time']];
        }
        if (!empty($param['end_time'])) {
            $where[] = ['a.created_at', "<=", $param['end_time']];
        }

        $list = app('model_shareprize_user_record')
            ->from('ln_shareprize_user_record as a')
            ->join('ln_user_data as b', 'a.register_user_id', '=', 'b.user_id')
            ->select([
                'a.id', 'a.share_user_id', 'a.register_user_id', 'b.user_code', 'b.nickname',  'a.created_at'
            ])
            ->where($where)
            ->orderBy('a.id', 'desc')
            ->paginate($param['page_size'], ['*'], '', $param['page']);

        foreach ($list as &$value) {
            $shareUserInfo = app('model_user_data')->where(['user_id' => $value->share_user_id])->first();
            $value['share_user_code'] =  $shareUserInfo->user_code;
            $value['share_user_nickname'] =  $shareUserInfo->nickname;
            $value['money'] = app('model_user_recharge')->where(['user_id' => $value->register_user_id, 'status' => 2])->sum('money');
            $value['last_login_time'] = app('model_user')->where(['user_id' => $value->register_user_id])->value('last_online_time');
            $value['last_login_time'] = date("Y-m-d H:i:s", $value['last_login_time']);
        }
        $res['list'] = $list;
        return Response::sendData($res);
    }
}
